/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aitsm;

import java.awt.HeadlessException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author minh
 */
public class XMLConfig {
    public void saveConfig (String serverName,int port, String databaseName, String userSQL, String passQSL)
    {
        try {
        Properties p = new Properties();        
        p.setProperty("server", serverName);
        p.setProperty("Port", Integer.toString(port));
        p.setProperty("Data", databaseName);
        p.setProperty("user", userSQL);
        p.setProperty("pass", passQSL);
        p.storeToXML(new FileOutputStream("config.xml"), "last updated at "+ new Date().toString());
        
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static Connection con = null;
    
    public String initConnection(String serverName, int port, String databaseName, String userSQL, String passQSL){
        try {
             
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            String url = "jdbc:sqlserver://"+ serverName +":" + port + ";" + 
                    "databaseName = " + databaseName + ";" +
                    "username = "+ userSQL + "; password = " + passQSL;
//            ptionPane.showMessageDialog(null, url);
            this.con = DriverManager.getConnection(url);
//            
            if(this.con != null)
                JOptionPane.showMessageDialog(null, "Connect successful! ");
            
            
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | HeadlessException | SQLException ex) {
            ex.getMessage();
            JOptionPane.showMessageDialog(null, "Cannot connect to Server!", "Connection", JOptionPane.WARNING_MESSAGE);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
}
